package com.inspur.gs.sbssdl.sbssgl.yxrj.yxrj.back.bizactions;

import com.inspur.edp.bef.api.be.IBEManagerContext;
import com.inspur.edp.bef.api.be.IBEService;
import com.inspur.edp.bef.api.be.IBusinessEntity;
import com.inspur.edp.bef.api.lcp.ILcpFactory;
import com.inspur.edp.bef.api.lcp.IStandardLcp;
import com.inspur.edp.bef.api.services.IBefSessionManager;
import com.inspur.edp.bef.engine.entity.AssociationInfo;
import com.inspur.edp.bef.spi.action.AbstractManagerAction;
import com.inspur.edp.cef.entity.condition.EntityFilter;
import com.inspur.edp.cef.entity.condition.FilterCondition;
import com.inspur.edp.cef.entity.entity.IEntityData;
import io.iec.edp.caf.commons.utils.SpringBeanUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class sbssYxrjDeleteRecordMgrAction extends AbstractManagerAction<String> {
    private String id;

    public sbssYxrjDeleteRecordMgrAction(IBEManagerContext managerContext, String id) {
        super(managerContext);
        this.id = id;
    }

    @Override
    public void execute() {
        SpringBeanUtils.getBean(IBefSessionManager.class).createSession();
        String yxrjId = "0e7d4ff6-c98f-47b8-98bc-2a96410887dd";
        String sbtsId = "35bf7f5c-48ae-499f-b7f7-2f011a4fb3c8";

        try {
            ILcpFactory iLcpFactory = SpringBeanUtils.getBean(ILcpFactory.class);
            IStandardLcp lcp = iLcpFactory.createLcpByBEId(yxrjId);
            IEntityData data = lcp.retrieve(id).getData();

            String jlrq = data.getValue("rq").toString(); //记录日期
            AssociationInfo sbidInfo = (AssociationInfo) data.getValue("sbid");
            String sbid = sbidInfo.getValue("sbid").toString();

            IStandardLcp tslcp = iLcpFactory.createLcpByBEId(sbtsId);
            EntityFilter filter = new EntityFilter();
            ArrayList<FilterCondition> conditionList = (ArrayList<FilterCondition>) FilterCondition.builder()
                    .andEqual("devId", sbid)
                    .andEqual("rq", jlrq)
                    .build();
            filter.setFilterConditions(conditionList);
            List<IEntityData> dataList = tslcp.query(filter);
            List<String> tsIds = dataList.stream()
                    .map(iEntityData -> iEntityData.getID())
                    .collect(Collectors.toList());
            if (dataList.size() < 1) {
                throw new RuntimeException("获取到的台时记录不存在，请重试！");
            }
            lcp.delete(id);
            tslcp.delete(tsIds);

            lcp.save();
            tslcp.save();

        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        } finally {
            SpringBeanUtils.getBean(IBefSessionManager.class).closeCurrentSession();
        }
    }

    private IBusinessEntity getEntity(String dataId) {
        return (IBusinessEntity) super.getBEManagerContext().getEntity(dataId);
    }

    private IBEService getMgr() {
        return (IBEService) super.getBEManagerContext().getBEManager();
    }
}